<template>
	<view>
		<!-- main顶部 -->
		<view class="main-top">
			<!-- 导航 -->
			<view class="top" :style="{height:statusBarHeight+'px',width:windowWidth+'px'}"></view>
			<view class="box-bg" :style="{height:navbarHeight+'px'}">
				<text style="color: #FFFFFF;font-size: 36rpx;">VIP专享</text>
			</view>
			<!-- 基本信息 -->
			<view class="lb-vip">
				<view class="img-bg">
					<image src="/static/bg-imgs/vip-0.png" mode=""></image>
				</view>
				<view class="vip-left">
					<view class="vip-top">
						<view class="img">
							<image src="/static/my/vip.png" mode=""></image>
						</view>
						<view class="text">
							<text>VIP会员中心</text>
							<text>I {{fmtFormat(user_info.vip_expire_time)}}到期</text>
						</view>
					</view>
					<view class="vip-equity">

						<!-- <view class="equity-item" v-for="(item,index) in 3" :key="index">
							<image src="/static/icon/5.png" mode=""></image>
							<text>权益1</text>
						</view> -->
					</view>
				</view>

				<view class="vip-right" @click="goTo('/pages/vip-center/vip-buy-list/vip-buy-list')">
					购买记录
				</view>
			</view>
		</view>
		<!-- 会员列表 -->
		<view class="vip-list">
			<!-- 标题 -->
			<view class="list-title">
				开通VIP
			</view>
			<!-- 列表 -->
			<view class="list-box">
				<view class="box-item" :class="isActive == index?'active':''" v-for="(item,index) in listData"
					:key="index" @click="clickActive(index,item)">
					<!-- 	<view class="img">
						<image v-if="index == 0" src="/static/images/new-p.png" mode=""></image>
						<image v-else-if="index == 2" src="/static/images/tui-j.png" mode=""></image>
					</view> -->
					<view class="label">
						{{item.name}}
					</view>
					<view class="price">
						{{item.price}}
					</view>
					<view class="o-price">
						{{item.old_price}}
					</view>
				</view>
			</view>

			<view v-if="listData&&listData.length">
				<image class="vip-img" :src="getImgUrl(listData[isActive].image)" mode="widthFix"></image>
				<view class="vip-tip">
					<u-parse :content="listData[isActive].remark"></u-parse>
				</view>
			</view>
			<!-- 按钮 -->
			<view class="lb-btn" @click="formSubmit">
				{{user_info.is_vip == 1 ? '续费' : '立即开通'}}
			</view>
		</view>
		<tabbar :numb="2"></tabbar>
	</view>
</template>

<script>
	import {
		getVipPriceApiList,
		getVipPriceApiSub,
		getPayApiSign
	} from "@/api/apis/comm.js"
	import {
		setImgUrl,
		fmtFormat
	} from "@/utils/comm.js"
	import tabbar from '@/components/tabbar.vue'
	export default {
		components: {
			tabbar
		},
		data() {
			return {
				// 设置状态栏默认值高度为20
				statusBarHeight: 20,
				// 设置导航栏的默认值为45
				navbarHeight: 45,
				// 设置手机宽度默认值为375
				windowWidth: 375,
				statusBarAndNavbarHeight: 65,

				isActive: 0,
				listData: [],
			}
		},
		onLoad() {
			this.getList();
		},
		methods: {
			getImgUrl(url) {
				return setImgUrl(url);
			},
			fmtFormat(time) {
				return time ? fmtFormat(time * 1000, 'yyyy-MM-dd') : '';
			},
			//跳转
			goTo(url) {
				if (url) {
					uni.navigateTo({
						url: url
					})
				}
			},
			//选择
			clickActive(i, t) {
				this.isActive = i;
			},
			//获取价格表
			getList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				getVipPriceApiList().then(res => {
					uni.hideLoading();
					if (res.code == 1) {
						this.listData = res.data
					}
				})
			},
			//提交订单
			formSubmit() {
				let _this = this;
				let vip_id = this.listData[this.isActive].id;
				uni.showLoading({
					title: "请求中",
					mask: true
				});
				getVipPriceApiSub({
					vip_id: vip_id
				}).then(res => {
					uni.hideLoading();
					if (res.code == 1) {
						// _this.$u.toast('成功!');
						getPayApiSign({
							order_id: res.data.order_ids,
							type: 1,
						}).then(res => {
							// console.log(res)
							if (res.code == 1) {
								let sign = res.data;
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: sign.timeStamp.toString(),
									nonceStr: sign.nonceStr,
									package: sign.package,
									signType: 'MD5',
									paySign: sign.paySign,
									success: function(res) {
										console.log('success:' + JSON.stringify(res));
										uni.showModal({
											showCancel: false,
											title: "温馨提示",
											content: "支付成功",
											success() {
												uni.navigateTo({
													url: '/pages/mine/mine'
												})
											}
										})
									},
									fail: function(err) {
										uni.showToast({
											title: "取消支付",
											icon: 'none'
										})
										uni.navigateTo({
											url: '/pages/xi-hu/home/home'
										})
										// console.log(err)
									}
								});
							}
						})
					} else {

					}
				})
			}
		},
		created() {
			// 同步获取系统（手机）信息
			const info = uni.getSystemInfoSync();
			// 动态设置状态栏的高度
			this.statusBarHeight = info.statusBarHeight;
			// 动态设置手机宽度
			this.windowWidth = info.windowWidth;
			// 获取胶囊的位置
			// #ifdef MP
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			// #endif

			// #ifdef H5
			const menuButtonInfo = {
				bottom: 20,
				top: 20
			}
			// #endif

			// console.log(info);
			// 动态设置导航栏的位置
			this.navbarHeight = (menuButtonInfo.bottom - this.statusBarHeight) + (menuButtonInfo.top - this
				.statusBarHeight);
			this.statusBarAndNavbarHeight = this.navbarHeight + this.statusBarHeight;
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.vip-tip {
		padding: 15rpx;
		border: 1px dashed #68D3A0;
		margin: 30rpx 0;
		border-radius: 10rpx;
		font-size: 28rpx;
	}

	.vip-img {
		width: 100%;
		border-radius: 10rpx;
	}

	.main-top {
		// height: 468rpx;
		background: linear-gradient(to bottom right, #7DE6B4, #68D3A0, #76D6A8, #71CBA0);

		.top {
			// background: linear-gradient(100deg, #00d5ff, #00aaff);
		}

		.box-bg {
			// background: linear-gradient(100deg, #00d5ff, #00aaff);
			padding: 5rpx 10rpx;
			display: flex;
			gap: 10rpx;
			place-items: center;
			justify-content: center;

			image {
				width: 50rpx;
				height: 50rpx;
				background-color: antiquewhite;
				border-radius: 510rpx;
			}
		}

		//会员信息
		.lb-vip {
			width: calc(100% - 64rpx);
			// height: 200rpx;
			// background: linear-gradient(124deg, rgba(247, 207, 158, 0.8) 0%, #E8B069 100%);
			// box-shadow: 0rpx 2rpx 4rpx rgba(0, 0, 0, 0.16);
			border-radius: 16rpx;
			margin: 34rpx auto 0;
			padding: 24rpx 32rpx 44rpx;
			// padding: 12rpx 32rpx 18rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			// justify-content: center;
			position: relative;

			.img-bg {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
			}

			.vip-left {

				// z-index: 10000;
				display: flex;
				flex-direction: column;
				// justify-content: space-between;
				justify-content: center;
				position: relative;

				.vip-top {

					display: flex;
					flex-direction: row;
					align-items: center;

					// justify-content: center;

					.img {
						width: 72rpx;
						height: 72rpx;

					}


					.text {
						display: flex;
						flex-direction: row;
						align-items: flex-end;
						padding-top: 10rpx;

						text {
							font-size: $main-font-28;
							font-weight: bold;
							color: #7C562D;
							padding-left: 12rpx;

							&:nth-of-type(2) {
								font-size: $main-font-20;
								font-weight: 400;
								padding-bottom: 4rpx;
							}
						}
					}
				}

				.vip-equity {
					// position: relative;
					display: flex;
					flex-direction: row;
					align-items: center;
					padding: 12rpx 0 0 10rpx;



					.equity-item {
						display: flex;
						flex-direction: row;
						align-items: center;
						padding-right: 24rpx;

						image {
							width: 48rpx;
							height: 48rpx;
						}

						text {
							font-size: $main-font-18;
							font-weight: 400;
							color: #B88443;
							margin-left: 2rpx;

						}
					}
				}

			}

			.vip-right {
				position: relative;
				width: 128rpx;
				height: 48rpx;
				border: 1rpx solid;
				background: linear-gradient(124deg, #B07F42 0%, #976737 100%);
				opacity: 0.8;
				border-radius: 72rpx;
				font-size: $main-font-24;
				font-weight: 400;
				line-height: 46rpx;
				text-align: center;
				color: $main-font-color-white;
			}

		}
	}

	//
	.vip-list {
		position: relative;
		width: 750rpx;
		// height: 452rpx;
		background: rgba(255, 255, 255, 1);
		border-radius: 16rpx 16rpx 0rpx 0rpx;
		margin-top: -36rpx;
		padding: 36rpx;
		box-sizing: border-box;

		.list-title {
			font-size: $main-font-28;
			font-weight: bold;
			color: $main-font-color;
		}

		.list-box {
			display: flex;
			flex-direction: row;
			width: 100%;

			// overflow: hidden;
			overflow-x: scroll;
			// white-space: nowrap;
			padding: 28rpx 0;

			.box-item {
				width: 204rpx;
				height: 220rpx;
				background: rgba(253, 251, 247, 0.39);
				border: 2px solid #CCCCCC;
				box-shadow: 0rpx 2rpx 4rpx rgba(0, 0, 0, 0.16);
				opacity: 1;
				border-radius: 16rpx;
				margin-right: 16rpx;
				flex-shrink: 0;
				box-sizing: border-box;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;
				padding: 44rpx 0;
				box-sizing: border-box;

				position: relative;

				.img {
					position: absolute;
					width: 100rpx;
					height: 36rpx;
					left: -4rpx;
					top: -12rpx
				}



				.label {
					font-size: $main-font-28;
					font-weight: bold;
					line-height: 40rpx;
					color: #7C562D;
				}

				.price {
					font-size: $main-font-36;
					font-weight: bold;
					line-height: 44rpx;
					color: #7C562D;

					&::before {
						content: '¥';
						font-size: $main-font-24;
						margin-right: 2rpx;
					}
				}

				.o-price {
					font-size: $main-font-24;
					font-weight: bold;
					line-height: 34rpx;
					color: $main-font-color-grey9;
					text-decoration: line-through;

					&::before {
						content: '¥';
						margin-right: 2rpx;
					}
				}

				// display: flex;
			}

			.active {
				border: 2px solid #AA7E4F;
			}

		}

		.lb-btn {
			width: calc(100% - 80rpx);
			height: 80rpx;
			background: linear-gradient(319deg, #F0D8BB 0%, #E9B067 100%);
			border-radius: 44rpx;
			font-size: 28rpx;
			color: #FFFFFF;
			line-height: 78rpx;
			text-align: center;
			margin: 0 auto;

		}
	}
</style>